#ifndef __RING_BUFFER_H
#define __RING_BUFFER_H

#include "stdbool.h"	

#define __RING_BUFFER_VERSION  100


#ifndef MINE_DATA_DEFINE
#define MINE_DATA_DEFINE

typedef unsigned int    u32;
typedef unsigned short  u16;
typedef unsigned char   u8;

typedef signed int 	 s32;
typedef signed short s16;
typedef signed char  s8;

#endif

struct buf_pubilc_def
{
	bool is_full;
	u16  size;
	u16  get_idx;
	u16  put_idx;
};	
	
	
struct ring_buffer_def
{
	struct buf_pubilc_def parent;
	u8*dat;
};	

		
	
#define IS_NON_EMPTY( obj )	(( obj.get_idx != obj.put_idx )||( obj.is_full == true ))
#define IS_EMPTY( obj )		(( obj.get_idx == obj.put_idx )&&( obj.is_full == false ))



#define IS_GET_MAX( obj )	( obj.get_idx >= obj.size )
#define IS_PUT_MAX( obj )	( obj.put_idx >= obj.size )


#define ADD_PUT_INDEX( obj ) if ( ++obj.put_idx >= obj.size )  \
	{ 														   \
		obj.put_idx = 0 ;									   \
	}														   \
	obj.is_full = false 

#define ADD_GET_INDEX( obj ) if ( ++obj.get_idx >= obj.size )   \
	{													    	\
		obj.get_idx = 0 ;										\
	}															\
	if ( obj.get_idx == obj.put_idx )							\
	{															\
		obj.is_full = true ;									\
	}

#define RING_BUFFER_RESET( obj )				\
	obj.is_full = false ;						\
	obj.get_idx = 0 ;							\
	obj.put_idx = 0

#define RING_BUFFER_INIT( obj, num, name )			\
	static u8 name##buf[num] ;						\
	obj.parent.size = num ;						    \
	RING_BUFFER_RESET(obj.parent);					\
	obj.dat = name##buf
	
#define RING_BUFFER_INIT_TYPE( obj, num, name, data_type ) \
	static data_type name##buf[num] ;					   \
	obj.parent.size = num ;    						       \
	RING_BUFFER_RESET(obj.parent);						   \
	obj.dat = (data_type*)name##buf	
	
	
#define WRITE_RING_BUFFER( buf, data )	 \
	buf->dat[ buf->parent.get_idx ] = data ; \
	ADD_GET_INDEX( buf->parent )
	
#define READ_RING_BUFFER( buf )  buf->dat[ buf->parent.put_idx ];	\
	ADD_PUT_INDEX( buf->parent )


/* 不会产生溢出的问题 */
#define RING_BUFFER_WRITE_BYTE( buf, byte )		\
{												\
	uint16_t wrIdx = buf->parent.get_idx ;		\
	wrIdx++;									\
	if ( wrIdx >= buf->parent.size )			\
	{											\
		wrIdx = 0 ;								\
	}											\
	if ( wrIdx != buf->parent.put_idx )			\
	{											\
		buf->dat[ buf->parent.get_idx ] = byte ;\
		buf->parent.get_idx = wrIdx ;			\
	}											\
}

#ifdef __cplusplus
extern "C" {
#endif
void null_task(void);	
s16 get_empty_ring_buffer( struct buf_pubilc_def *index ); /* 此函数之后不再支持使用 */
s16 get_free_ring_buffer(  struct buf_pubilc_def *index );
s16 get_hold_ring_buffer(  struct buf_pubilc_def *index );

char*string_equal_compare( struct ring_buffer_def *buf, u16 start_addr, const char*str );
void ring_buffer_get_data( u8*obj, u16 start_addr, u16 len, struct ring_buffer_def*buf );
void cpy_data_to_ring_buffer( struct ring_buffer_def*buf, u8*dat, u16 num );  /* 废弃，之后的版本不再使用 */
void get_data_from_ring_buffer( struct ring_buffer_def*buf, u8*obj, u16 len );

void ring_buffer_clear( struct buf_pubilc_def *obj );
u16 ring_buffer_get_use_length( struct buf_pubilc_def *obj );
u16 ring_buffer_get_surplus_length( struct buf_pubilc_def *obj );
void ring_buffer_write_block( struct ring_buffer_def*buf, u8*dat, u16 num );
void ring_buffer_read_block( struct ring_buffer_def*buf, u8*dat, u16 num );
#ifdef __cplusplus
}
#endif

/****************** 兼容老版本，禁止使用以下的宏定义 *******************/
#define ring_buffer_typedef struct ring_buffer_def
#define get_index get_idx
#define put_index put_idx
#define buf_size  size
/************************************************************************/
	
	
#endif





	
